//
//  ATOM.m
//  TuiTe
//
//  Created by 张 霄鹤 on 10-4-23.
//  Copyright 2010 xhzhang. All rights reserved.
//

#import "ATOM.h"


@implementation ATOM

@synthesize title;
@synthesize refresh;
@synthesize next;

- (id)init{
	if(self = [super init]){
		entries = [[NSMutableArray alloc] init];
	}
	return self;
}

- (void)addEntry:(Entry *)en{
	if(en){
		[entries addObject:en];
	}
}

- (NSInteger)entriesCount{
	return [entries count];
}

- (Entry *)entryAtIndex:(NSInteger)index{
	Entry *ret = nil;
	if(index > -1){
		NSInteger clen = [entries count];
		if(index < clen){
			ret = [entries objectAtIndex:index];
		}
	}
	return ret;
}

- (void)clear{
	[entries removeAllObjects];
}

- (void)parse:(Element *)el{
	Node *nd;
	if(el){
		nd = [el getChildByName:@"title"];
		self.title = [[nd getChildByName:@"#TEXT"] getValue];
		
		NSArray *arr = [el getElementsByName:@"link"];
		if(arr){
			NSString *av;
			for (Element *ea in arr) {
				av = [ea getAttribute:@"rel"];
				if([av isEqualToString:@"refresh"]){
					self.refresh = [ea getAttribute:@"href"];
				}else if([av isEqualToString:@"next"]){
					self.next = [ea getAttribute:@"href"];
				}
			}
		}
		
		// TODO 解析Entry
		
		arr = [el getElementsByName:@"entry"];
		if(arr){
			Entry *en;
			for(Element *ea in arr){
				en = [[Entry alloc] init];
				[en parse:ea];
				[entries addObject:en];
				[en release];
			}
		}
	}
}

- (void)dealloc{
	[title release];
	[refresh release];
	[next release];
	[entries release];
	[super dealloc];
}

@end
